/*
 * Copyright 2016-2017 TVI Go Easy.
 * Created on 2017/10/27 10:13
 */
package com.gitee.mechanic.shiro.utils;

import com.gitee.mechanic.core.enums.HttpCode;
import com.gitee.mechanic.shiro.exception.LoginException;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;

/**
 * 登录工具类
 *
 * @author mechanic
 */
public class LoginUtils {
    /**
     * 微信用户登录
     *
     * @param account  账户
     * @param password 密码
     * @return 登录成功返回true，否则抛出异常
     */
    public static Boolean login(String account, String password) {
        UsernamePasswordToken token = new UsernamePasswordToken(account, password);
        token.setRememberMe(false);
        Subject subject = SecurityUtils.getSubject();
        try {
            subject.login(token);
            return subject.isAuthenticated();
        } catch (AuthenticationException e) {
            throw new LoginException(HttpCode.ACCOUNT_PASSWORD_ERROR);
        }
    }
}
